<script lang="ts" setup>
import { hBtns, UiDataView ,openModal} from '../../utils'
import { Delete, Plus } from '@element-plus/icons-vue'
import { onMounted, ref } from 'vue'
import { DataViewRef, PageFetchAction, TableCellBtn, TableColumnConfig } from '../../utils/types'

const tableRef: DataViewRef = ref()

const columns = [
  { label: '类型', prop: 'ruleName' },
  { label: '描述' },
  { label: '字典类型' },
  { label: '备注信息' },
  { label: '创建时间' },
  {
    label: '操作',
    fixed: 'right',
    width: 200,
    render (h, { row }) {
      return hBtns([
        { label: '编辑', onClick: onEdit.bind(null, row) },
        { label: '删除', onClick: onDelete.bind(null, [ row ]) },
        { label: '字典项', onClick: onItemEdit.bind(null, row) }  
      ] as TableCellBtn[])
    }
  }
] as TableColumnConfig<any>[]

const fetchAction: PageFetchAction = params => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        total: 2,
        data: [{ ruleName: 'test' }, { ip: '168.12.3.4' }]
      })
    }, 500)
  })
}

const onDelete = (rows: any[]) => {
  tableRef.value.deleteRows(() => {
    return Promise.resolve()
  })
}
const onItemEdit = (row?: any) => {
  openModal(() => import('./DictItem.vue'), {
    row,
    title: '字典项管理',
    onSubmitted ({ form }) {
    }
  })
}

const onEdit = (row?: any) => {
  openModal(() => import('./DictEdit.vue'), {
    row,
    title: `${row ? '编辑' : '新增'}`,
    onSubmitted ({ form }) {
    }
  })
}
onMounted(async () => {
  await tableRef.value.setInitForm({ namelist: '1' })
  tableRef.value.getData()
})
</script>

<template>
  <UiDataView ref="tableRef" :columns="columns" :fetch-action="fetchAction">
    <template #search="{ form, onSearch }">
      <ElFormItem label="类型">
        <ElInput v-model="form.apiName" @keyup.enter="onSearch" />
      </ElFormItem>
      <ElFormItem label="字典类型">
        <ElSelect clearable filterable></ElSelect>
      </ElFormItem>
      <!-- <ElFormItem label="字典类型">
        <ElSelect v-model="form.apiType" clearable filterable>
          <ElOption v-for="_ in apiTypes" :key="_.value" v-bind="_" />
        </ElSelect>
      </ElFormItem> -->
    </template>
    <template #actions="{ selectedRows }">
      <ElButton :icon="Plus" type="primary" @click="onEdit()">新增</ElButton>
      <ElButton
        type="danger"
        :icon="Delete"
        :disabled="selectedRows.length < 1"
        @click="onDelete(selectedRows)">删除</ElButton>
    </template>
  </UiDataView>
</template>./DictItem.vue